//
//  PreuViewController.m
//  SurtCasa
//
//  Created by ShimiFriends on 11/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PreuViewController.h"
#import "SuccesViewController.h"
#import "FailViewController.h"
#define PreuViewControllerNibName @"PreuViewController"


@implementation PreuViewController
@synthesize name, edat, quotes, ingressos, prestecs;

+(PreuViewController*) preuViewControllerWithName:(NSString*)name withEdat:(NSString*) edat withIngressos:(NSString*)ingressos withQuotes:(NSString*)quotes withPrestecs:(NSString*)prestec
{
    PreuViewController* retController = [[PreuViewController alloc] initWithNibName:PreuViewControllerNibName bundle:nil];
    [retController setName:name];
    [retController setEdat:edat];
    [retController setIngressos:ingressos];
    [retController setQuotes:quotes];
    [retController setPrestecs:prestec];
    return [retController autorelease];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Nom: %@, Edat: %@, Ingressos: %@, Quotes: %@, Prestecs: %@", name, edat, ingressos, quotes, prestecs);
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)dismissKeyboard: (id)sendr {
    [sendr resignFirstResponder];
}

-(IBAction)endavant:(id)sendr
{
    float iedat = [edat floatValue];
    float iingressos = [ingressos integerValue] * 12;
    float hipoteca;
    if ( iedat < 33 && iingressos < 32000) {
        hipoteca = ([preu.text floatValue] * 1.055 - [entrada.text floatValue]) * 1.045;
    }
    else
    {
        hipoteca = ([preu.text floatValue] * 1.085 - [entrada.text floatValue]) * 1.045;
    }

    float maxim = 75.0 - [edat floatValue];
    if (maxim > 40.0) maxim = 40.0;

    
    float resultat = hipoteca * 4.0/12.0;
    float resultat2 = 100*(1.0-pow((1.0+0.04/12.0),-(maxim*12.00)));
    float mes = resultat/resultat2;

    float maximmes = [ingressos floatValue]*0.4;
    
    NSLog(@"Mensualitat: %f Maxim: %f",mes, maximmes);
    
    
    if(maximmes > mes) 
    {
        NSString* mensualitat = [NSString stringWithFormat:@"%.02f",mes];
        SuccesViewController* ivi= [SuccesViewController succesViewControllerWithName:name withMensual:mensualitat];
        [[self navigationController] pushViewController:ivi animated:YES];
    }
    else { 
        float calc = (maximmes*resultat2/(4.0/12.0));
        float diferencia = hipoteca - calc;
        float pismax;
        if ( iedat < 33 && iingressos < 32000) {
            pismax = (calc/1.045 + [entrada.text floatValue])/1.055;
            NSLog(@"hipoteca: %f pismax: %f",hipoteca, pismax);
        }
        else
        {
            pismax = (calc/1.045 + [entrada.text floatValue])/1.085;
            NSLog(@"hipoteca: %f pismax: %f",hipoteca, pismax);
        }
        NSString* mensualitat = [NSString stringWithFormat:@"%.02f",maximmes];
        NSString* diff = [NSString stringWithFormat:@"%.02f",diferencia];
        NSString* nou = [NSString stringWithFormat:@"%.02f",pismax];
        FailViewController* ivi= [FailViewController failViewControllerWithName:name withRestant:diff withNou:nou withMensual:mensualitat];
        [[self navigationController] pushViewController:ivi animated:YES];
    }
}

@end
